package com.mypage.view.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.dao.impl.ApprovalEngineImpl;
import com.cloudcc.mobile.dialog.CallLogLoadingDialog;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.view.activity.TaskManagementActivty;
import com.cloudcc.mobile.weight.CustomProgressDialog;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.litesuits.android.async.Log;
import com.mypage.utils.NetworkDetector;

import org.json.JSONException;
import org.json.JSONObject;

public class ApproveActivityDialog extends Activity implements OnClickListener {
	private ApprovalEngineImpl engine = new ApprovalEngineImpl();
	private Button mCancel;
	private Button mSure;
	private Button mPerson;
	private EditText mContent;
	private String url = "www.baidu.com";
	private String personId;
	private String content;
	private String workItemId;
	private String isRequired = "false";
	Context context;
	private CustomProgressDialog progressDialog;
	CallLogLoadingDialog callLogLoadingDialog ;
	private String isNew = "";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.approval_dialog);
		progressDialog = CustomProgressDialog.createDialog(this);
		setFinishOnTouchOutside(false);
		Intent intent = getIntent();
		workItemId = intent.getStringExtra("workItemId");
		isRequired = intent.getStringExtra("isRequired");
		isNew = intent.getStringExtra("weizhi");
		initViews();
//		if("false".equals(isRequired)){
//			zidong();
//		}
		initListener();
	}

	/**
	 * dialog开始显示
	 */
	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(context);
			progressDialog.setCancelable(true);
			progressDialog.setCanceledOnTouchOutside(false);
			// progressDialog.setMessage("正在加载中...");
		}
		progressDialog.show();
	}

	/**
	 * 停止dialog
	 */
	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	private void initViews() {
		// TODO Auto-generated method stub
		callLogLoadingDialog  = new CallLogLoadingDialog(this);
		mCancel = (Button) findViewById(R.id.quxiaocanle);
		mSure = (Button) findViewById(R.id.quedingsure);
		mPerson = (Button) findViewById(R.id.btnSelectorPerson);
		mContent = (EditText) findViewById(R.id.editTextContent);
		mSure.setText("批准");
	}

	private void initListener() {
		// TODO Auto-generated method stub
		mCancel.setOnClickListener(this);
		mSure.setOnClickListener(this);
		mPerson.setOnClickListener(this);

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.btnSelectorPerson:
			Intent intent = new Intent();
			intent.setClass(ApproveActivityDialog.this,
					AtContaxtActivity_.class);

			startActivityForResult(intent, 0);

			// Toast.makeText(ApproveActivityDialog.this, "选择批准人",
			// Toast.LENGTH_SHORT).show();
			break;
		case R.id.quxiaocanle:
			finish();
			// Toast.makeText(ApproveActivityDialog.this, "取消",
			// Toast.LENGTH_SHORT)
			// .show();
			break;

		case R.id.quedingsure:

			content = mContent.getText().toString().trim();// 审批备注
			if("true".equals(isRequired)){
				if(content.length()<=0){
					Toast.makeText(ApproveActivityDialog.this, getResources().getString(R.string.bbixueizhu),
							Toast.LENGTH_SHORT).show();
					return;
				}
			}
			// personId这是需要传的用户id
			// if (content.length() == 0) {
			// Toast.makeText(ApproveActivityDialog.this, "审批备为空....",
			// Toast.LENGTH_SHORT).show();
			//
			// } else if (personId == null) {
			// Toast.makeText(ApproveActivityDialog.this, "批准人为空....",
			// Toast.LENGTH_SHORT).show();
			//
			// } else
			if(content.length()>500){
				Toast.makeText(ApproveActivityDialog.this, "不可以超过500字",
						Toast.LENGTH_SHORT).show();
				return;
			}
			if (!NetworkDetector.detect(ApproveActivityDialog.this)) {
				Toast.makeText(ApproveActivityDialog.this, "当前网络不可用....",
						Toast.LENGTH_SHORT).show();
			} else {
				if("xiangqing".equals(isNew)) {
					mSure.setText("处理中");
					netWor2(content, personId);
					progressDialog.setCanceledOnTouchOutside(false);
					startProgressDialog();
					callLogLoadingDialog.show();
					callLogLoadingDialog.settext("处理中...");
				}else{
					mSure.setText("处理中");
					netWor2New(content, personId);
					progressDialog.setCanceledOnTouchOutside(false);
					startProgressDialog();
					callLogLoadingDialog.show();
					callLogLoadingDialog.settext("处理中...");
				}
			}

			break;
		}

	}
public void zidong(){
	netWor2(content, personId);
	startProgressDialog();
	callLogLoadingDialog.show();
	callLogLoadingDialog.settext("处理中...");
}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		switch (resultCode) {
		case RESULT_OK:
			String name = data.getStringExtra("name");
			personId = data.getStringExtra("userId");
			Log.d("bbb", name);
			mPerson.setText(name.substring(0, name.length() - 1));
			break;

		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);

	}

	public void netWor2(String content, String personId) {
		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();

		params.addBodyParameter("binding", RunTimeManager.getInstance()
				.getServerBinding());

		params.addBodyParameter("serviceName", "doApproved");

//		params.addBodyParameter("workItemId", workItemId);
////		params.addBodyParameter("actionType", "Approved");
//		params.addBodyParameter("comments", content);
		params.addBodyParameter("data", createJson());
		Log.d("hahahha", UrlManager.getInterfaceUrl() + params);
		utils.send(HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						stopProgressDialog();
						Intent intent = new Intent(ApproveActivityDialog.this,
								TaskManagementActivty.class);
//						if(workItemId.length()>30) {
//							intent.putExtra("chongxin", "chongxinmore");
//						}else{
						intent.putExtra("chongxinshibai","chongxin");
//						}
						startActivity(intent);
						sendBroadcast(new Intent().setAction("finish"));
						finish();
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						String result = arg0.result;
						Log.d("-------------------------", result);
						sendBroadcast(new Intent().setAction("taskfinish"));
//						stopProgressDialog();
						callLogLoadingDialog.dismiss();
						Intent intent = new Intent(ApproveActivityDialog.this,
								TaskManagementActivty.class);
						TaskManagementActivty.instance.finish();
						try {
							ApprovalPendingActivity.instance.finish();
						} catch (Exception e) {
							e.printStackTrace();
						}
						intent.putExtra("taskfinish","xiangqingpizhun");
						startActivity(intent);
//						sendBroadcast(new Intent().setAction("finish"));
						finish();
					}
				});

	}


	public String createJson() {
		JSONObject object = new JSONObject();
		try {

//			params.addBodyParameter("workItemId", workItemId);
//			params.addBodyParameter("comments", content);
			object.put("workItemId", workItemId);
			object.put("comments", content);
			// Log.d("/////////////////", object.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return object.toString();

	}

	public void netWor2New(String content, String personId) {
		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();

		params.addBodyParameter("userId",
				UserManager.getManager().getUser().userId);
		params.addBodyParameter("binding", RunTimeManager.getInstance()
				.getServerBinding());

		params.addBodyParameter("serviceName", "ajaxProcessMassThread");

		params.addBodyParameter("workItemId", workItemId);
		params.addBodyParameter("actionType", "Approved");
		params.addBodyParameter("comments", content);
		params.addBodyParameter("fprId", personId);
		Log.d("hahahha", UrlManager.getInterfaceUrl() + params);
		utils.send(HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						stopProgressDialog();
						Intent intent = new Intent(ApproveActivityDialog.this,
								TaskManagementActivty.class);
//						if(workItemId.length()>30) {
//							intent.putExtra("chongxin", "chongxinmore");
//						}else{
						intent.putExtra("chongxinshibai","chongxin");
//						}
						startActivity(intent);
						sendBroadcast(new Intent().setAction("finish"));
						finish();
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						String result = arg0.result;
						Log.d("-------------------------", result);
						sendBroadcast(new Intent().setAction("taskfinish"));
//						stopProgressDialog();
						callLogLoadingDialog.dismiss();
						Intent intent = new Intent(ApproveActivityDialog.this,
								TaskManagementActivty.class);
						TaskManagementActivty.instance.finish();
						try {
							ApprovalPendingActivity.instance.finish();
						} catch (Exception e) {
							e.printStackTrace();
						}
						if(workItemId.length()>30) {
							intent.putExtra("zhuangtai", "jujue");
						}else{
							intent.putExtra("taskfinish","taskfinish");
						}
						startActivity(intent);
//						sendBroadcast(new Intent().setAction("finish"));
						finish();
					}
				});

	}

}
